package gupao.concurrency.concurrent.utils;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class PriorityBlockingQueueSample {

    public static void main(String[] args)  {
        List<String> stringList = Stream.of("orange","apple","lemon","banana").collect(Collectors.toList());
        List<NonComparableStudent> nonComparableList = Stream.of(
                    new NonComparableStudent("orange", 20),
                    new NonComparableStudent("apple", 15),
                    new NonComparableStudent("lemon", 25),
                    new NonComparableStudent("banana", 22))
                .collect(Collectors.toList());

        List<ComparableStudent> comparableList = Stream.of(
                        new ComparableStudent("orange", 20),
                        new ComparableStudent("apple", 15),
                        new ComparableStudent("lemon", 25),
                        new ComparableStudent("banana", 22))
                .collect(Collectors.toList());

//        test(stringList);
        test(nonComparableList); // exception
//        test(comparableList);
    }

    static <T> void test(List<T> objs){
        PriorityBlockingQueue<T> bq = new PriorityBlockingQueue<>(10);
        objs.forEach( bq::offer );
        while(bq.size() > 0){
            log.info("{}", bq.poll());
        }
    }


    @Setter
    @Getter
    @AllArgsConstructor
    @ToString
    static class NonComparableStudent{
        String name;
        int age;
    }

    @Setter
    @Getter
    @AllArgsConstructor
    @ToString
    static class ComparableStudent implements Comparable<ComparableStudent>{
        String name;
        int age;

        @Override
        public int compareTo(ComparableStudent o) {
            return Integer.compare(age, o.age);
        }
    }
}
